﻿using System;
using System.Drawing;
using System.Collections;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

//using System.Timers;
using WP.Commons;
using WP.Controls;
using WP.DB;
using System.Data.SqlClient;

namespace WP.Modules
{
    public partial class frmApprovalHistory : Form
    {
        public enum TableType
        {
            /// <summary>
            /// 半成品类别审核
            /// </summary>
            CagtegorySemi=0,

            /// <summary>
            /// 类别BOM审核
            /// </summary>
            CategoryBOM ,

            /// <summary>
            /// 产成品BOM审核
            /// </summary>
            ProductBOM
        }
            
        private DBAccess mdbcon = null;
        private TableType mTableType = new TableType();
        private string mstrKey = "";
        public frmApprovalHistory()
        {
            InitializeComponent();
        }

        public frmApprovalHistory( DBAccess dbcon , TableType tableType,string strKey)
        {
            InitializeComponent();
            mdbcon = dbcon;
            mTableType = tableType;
            mstrKey = strKey;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void InitGridItem()
        {
            this.dgvItem.Columns.Clear();
            this.dgvItem.Rows.Clear();

            DataGridViewTextBoxColumn colSeqNo = new DataGridViewTextBoxColumn();
            colSeqNo.HeaderText = "序号";
            colSeqNo.Visible = false;
            colSeqNo.Name = "SeqNo";
            colSeqNo.Width = 30;
            colSeqNo.ReadOnly = true;

            DataGridViewTextBoxColumn colType = new DataGridViewTextBoxColumn();
            colType.HeaderText = "类别";
            colType.Width = 50;
            colType.Name = "";
            colType.ReadOnly = false;
            colType.Visible = false;

            DataGridViewTextBoxColumn colUserName = new DataGridViewTextBoxColumn();
            colUserName.HeaderText = "操作人员";
            colUserName.Width = 150;
            colUserName.Name = "UserName";
            colUserName.ReadOnly = false;

            DataGridViewTextBoxColumn colOperDate = new DataGridViewTextBoxColumn();
            colOperDate.HeaderText = "日期";
            colOperDate.Width = 150;
            colOperDate.Name = "OperDate";
            colOperDate.ReadOnly = false;

            DataGridViewTextBoxColumn colOperType = new DataGridViewTextBoxColumn();
            colOperType.HeaderText = "操作类型";
            colOperType.Width = 100;
            colOperType.Name = "OperType";
            colOperType.ReadOnly = true;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            this.dgvItem.Columns.AddRange(new DataGridViewColumn[] { colSeqNo, colType, colUserName, colOperDate, colOperType, colRecID });

            this.dgvItem.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvItem.RowTemplate.Height = 20;
            this.dgvItem.RowHeadersWidth = 23;
            this.dgvItem.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvItem.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvItem.ColumnHeadersHeight = 30;
            this.dgvItem.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvItem.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvItem.AllowUserToResizeRows = false;
            FillGrid();
        }

        private void FillGrid()
        {
            this.dgvItem.Rows.Clear();
            string strSQL = "";
            string strSQLTmp = "";
            if (mTableType == TableType.CagtegorySemi)
            {
                strSQL = "Select A.*,B.CategoryName from mApproHisory A inner join rmCategory B on A.KeyID=B.CategoryID and A.tabletype='" + mTableType.ToString() + "' and KeyID='" + mstrKey + "'  order by recid desc";
            }
            else if (mTableType == TableType.CategoryBOM)
            {
                strSQL = "Select A.* from mApproHisory A where KeyID='" + mstrKey + "' and A.tabletype='" + mTableType.ToString() +"' order by recid desc";
            }
            else if (mTableType == TableType.ProductBOM)
            {
                strSQL = "Select A.* from mApproHisory A where KeyID='" + mstrKey + "' and A.tabletype='" + mTableType.ToString() + "' order by recid desc";
            }
            DataTable dtFormula = mdbcon.GetDataTable(strSQL);
            int intRow =1;
            foreach (DataRow drRow in dtFormula.Rows)
            {
                this.dgvItem.Rows.Add(); ;
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells["SeqNo"].Value = intRow .ToString() ;
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells["UserName"].Value = CvtFunc.ToString(drRow["UserName"]);
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells["OperDate"].Value = CvtFunc.ToString(drRow["OperDate"]);
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells["OperType"].Value = CvtFunc.ToString(drRow["OperType"]);
            }
            this.dgvItem.EndEdit();

        }

        private void frmApprovalHistory_Load(object sender, EventArgs e)
        {
            InitGridItem();
            FillGrid(); 
        }

    }
}
